package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {
    private MyBatisUtils(){}

    private static SqlSessionFactory factory;

    static {
        try {
            //获取核心配置文件
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //获取工厂对象
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取Sqlsession对象
    public static SqlSession getSqlSession(){
        return factory.openSession(true);
    }


}
